<template>
    <div class="roles">
        <el-card class="box-card">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item :to="{ path: '/index/users' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>权限管理</el-breadcrumb-item>
                <el-breadcrumb-item>角色列表</el-breadcrumb-item>
            </el-breadcrumb>
            <el-row style="margin: 10px 0;">
                <el-button @click="rolesInfoDialog=true">添加角色</el-button>
            </el-row>
            <el-dialog title="添加角色" :visible.sync="rolesInfoDialog">
                <el-form ref="rolesInfoForm" :model="rolesInfoForm">
                    <el-form-item 
                    prop="roleName"
                    label="角色名称" 
                    :label-width="formLabelWidth"
                    :rules="[
                        { required: true, message: '请输入角色名称', trigger: 'blur' }
                    ]">
                        <el-input v-model="rolesInfoForm.roleName" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item 
                    prop="roleDesc"
                    label="角色描述" 
                    :label-width="formLabelWidth"
                    :rules="[
                        { required: true, message: '请输入角色描述', trigger: 'blur' }
                    ]">
                        <el-input v-model="rolesInfoForm.roleDesc" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="rolesInfoDialog = false">取 消</el-button>
                    <el-button type="primary" @click="addRoles(rolesInfoForm)">确 定</el-button>
                </div>
            </el-dialog>
            <el-table :data="tableData" style="width: 100%">
                <el-table-column type="expand">
                    <template slot-scope="scope">
                        <template v-if="scope.row.children.length>0">
                            <el-row v-for="(item, index) in scope.row.children" :key="item.id">
                                <el-col :span="4">
                                    <el-tag closable @close="deleteRight(scope.row,item.id,0,index)">{{item.authName}}</el-tag>
                                    <i class="el-icon-arrow-right"></i>
                                </el-col>
                                <el-col :span="20">
                                    <el-row v-for="(ite, inde) in item.children" :key="ite.id">
                                        <el-col :span="4">
                                            <el-tag closable type="success" @close="deleteRight(scope.row,ite.id,1,index,inde)">{{ite.authName}}</el-tag>
                                            <i class="el-icon-arrow-right"></i>
                                        </el-col>
                                        <el-col :span="20">
                                            <el-tag v-for="(it, ind) in ite.children" :key="it.id" closable type="warning" @close="deleteRight(scope.row,it.id,2,index,inde,ind)">{{it.authName}}</el-tag>
                                        </el-col>
                                    </el-row>
                                </el-col>
                            </el-row>
                        </template>
                        <span v-else>没有权限</span>
                    </template>
                </el-table-column>
                <el-table-column type="index" label="#" width="50"></el-table-column>
                <el-table-column label="角色名称" prop="roleName"></el-table-column>
                <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" plain size="small" icon="el-icon-edit" circle @click="editRole(scope.row)"></el-button>
                        <el-button type="danger" plain size="small" icon="el-icon-delete" circle @click="removeRoles(scope.row.id)"></el-button>
                        <el-button type="success" plain size="small" icon="el-icon-check" circle @click="getAllRightsTree(scope.row)"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-dialog title="编辑用户" :visible.sync="editRoles">
                <el-form :model="editRolesForm">
                    <el-form-item 
                    prop="roleName"
                    label="角色名称" 
                    :label-width="formLabelWidth"
                    :rules="[
                        { required: true, message: '', trigger: 'blur' }
                    ]">
                        <el-input v-model="editRolesForm.roleName" autocomplete="off" :disabled="true"></el-input>
                    </el-form-item>
                    <el-form-item label="角色描述" :label-width="formLabelWidth">
                        <el-input v-model="editRolesForm.roleDesc" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="editRoles=false">取 消</el-button>
                    <el-button type="primary" @click="submitRoles(editRolesForm)">确 定</el-button>
                </div>
            </el-dialog>
            <el-dialog title="分配权限" :visible.sync="editRoleTreeDialog">
                <el-tree ref="tree" :data="data" default-expand-all show-checkbox node-key="id" :props="defaultProps"></el-tree>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="editRoleTreeDialog=false">取 消</el-button>
                    <el-button type="primary" @click="editRightIds()">确 定</el-button>
                </div>
            </el-dialog>
        </el-card>
    </div>
</template>
<script>
import { rolesList, deleteRight, getAllRightsTree, editRightIds, addRoles, submitRoles, catRoles, removeRoles} from '../../http/api'
export default {
    data() {
        return {
            tableData: [],
            editRoleTreeDialog: false,
            data: [],
            defaultProps: {
                children: 'children',
                label: 'authName'
            },
            roleId: '',
            rolesInfoDialog: false,
            rolesInfoForm: {roleName: '',roleDesc: ''},
            formLabelWidth: '100px',
            editRoles: false,
            editRolesForm: {id: '', roleName: '', roleDesc: ''}
        }
    },
    methods:{
        rolesList(){ //角色列表
            return rolesList().then(res=>{
                console.log(res.data);
                this.tableData=res.data;
            })
        },
        deleteRight(scopeRow, rightId, index, ...arr){ //删除角色指定权限
            return deleteRight(scopeRow.id,rightId).then(res=>{
                if (index==0) {
                    scopeRow.children.splice(arr[0],1);
                }else if (index==1) {
                    scopeRow.children[arr[0]].children.splice(arr[1],1)
                }else{
                    scopeRow.children[arr[0]].children[arr[1]].children.splice(arr[2],1)
                }
                this.$message({
                    showClose: true,
                    message: res.meta.msg,
                    type: 'success'
                })
            })
        },
        getAllRightsTree(obj){ //所有权限列表
            return getAllRightsTree().then(res=>{
                this.editRoleTreeDialog=true;
                // console.log(res);
                this.data=res.data;
                this.roleId = obj.id
                this.$nextTick(()=> {
                    this.$refs.tree.setCheckedNodes(obj.children);
                })
            })
        },
        async editRightIds(){ //角色授权
            const str = this.$refs.tree.getCheckedKeys().join(',');
            await editRightIds(this.roleId, str)
            this.editRoleTreeDialog=false;
            this.rolesList();
        },
        addRoles(){ //添加角色
            this.$refs.rolesInfoForm.validate(async(valid)=>{
                if (valid) {
                    const res = await addRoles(this.rolesInfoForm)
                    console.log(res);
                    this.$message({ 
                        showClose: true,
                        message: res.meta.msg,
                        type: 'success'
                    })
                    // 关闭添加用户弹框
                    this.rolesInfoDialog=false;
                    // 清空添加用户弹框的内容
                    this.$refs.rolesInfoForm.resetFields();
                    this.rolesList();
                }
            })
        },
        editRole(obj){ //编辑时显示角色信息
            this.editRoles=true;
            this.editRolesForm.id=obj.id;
            this.editRolesForm.roleName=obj.roleName;
            this.editRolesForm.roleDesc=obj.roleDesc;
        },
        submitRoles(obj){ //编辑提交角色
            this.editRoles=false;
            const {id,roleName,roleDesc} = obj;
            const data = {roleName, roleDesc}
            return submitRoles(id,data).then(res=>{
                // console.log(res);
                this.$message({ 
                showClose: true,
                message: res.meta.msg,
                type: 'success'
                })
                this.rolesList();
            })
        },
        removeRoles(id){ //删除角色
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                center: true
            }).then(() => {
                return removeRoles(id).then(res=>{
                    // console.log(res)
                    this.$message({
                        showClose: true,
                        type: 'success',
                        message: res.meta.msg
                    });
                    this.rolesList();
                })
            }).catch(() => {
                this.$message({
                    showClose: true,
                    type: 'info',
                    message: 'catch--'
                });
            });
        }
    },
    created(){
        this.rolesList();
    }
}
</script>
<style lang="scss" scoped>
.roles{
    height: 100%;
    .box-card{
        width: 100%;
        height: 100%;
    } 
}
</style>